#include <fstream>
#include <stdexcept>
#include <json/elements.h>
#include <json/reader.h>
#include "Level.hpp"

Level::Level()
{
}
       
void Level::loadFromFile(const std::string& fileName)
{
    std::string path = "media/levels/";
    path += fileName;
    
    std::ifstream file(path.c_str());
    
    if(!file.is_open())
        throw std::runtime_error("failed to open file : " + path);

	// we parse the file
	json::Object root;
	try 
	{
		json::Reader::Read(root,file);
		file.close();
	}
    catch (json::Reader::ScanException& e)
    {
		std::cerr << "Caught json::ScanException : " << e.what() << ", Line/offset: " << e.m_locError.m_nLine + 1 << '/' << e.m_locError.m_nLineOffset + 1 << " in file : " << fileName << std::endl;
        throw std::runtime_error("Error while scanning level file " + fileName + ".");
    }
    catch (json::Reader::ParseException& e)
    {
		std::cerr << "Caught json::ParseException : " << e.what() << ", Line/offset of token begin: " << e.m_locTokenBegin.m_nLine + 1 << '/' << e.m_locTokenBegin.m_nLineOffset + 1 << ", Line/offset of token end: " << e.m_locTokenEnd.m_nLine + 1 << '/' << e.m_locTokenEnd.m_nLineOffset + 1 << " in file : " << fileName << std::endl;
        throw std::runtime_error("Error while parsing level file " + fileName + ".");
    }
    
    
    const json::Object& doc = root;
    
    // background
    
    // obstacles
}
       
void Level::update(const float dt)
{

}
       
/*
Ball Level::createBall()
{

}
*/

void Level::draw(sf::RenderTarget &target)
{
/*
    target.draw(*m_background)
*/
}
